//
//  VC4UrlJson.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/24.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC4UrlJson: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let dict = NSMutableDictionary()
        dict.setValue("aaaaaaa", forKey: "transNo")
        dict.setValue("bbbbbbb", forKey: "userName")
        dict.setValue("1103", forKey: "appID")
        dict.setValue("00", forKey: "transType")
        dict.setValue("00", forKey: "authType")
        
        let device = NSMutableDictionary()
        device.setValue("ccccccccc", forKey: "deviceID")
        dict.setValue(device, forKey: "devices")
        
        let context = NSMutableDictionary()
        context.setValue(dict, forKey: "context")
        
        let body = try? JSONSerialization.data(withJSONObject: context, options: .fragmentsAllowed)
        
        let url = URL(string: "http://192.168.6.140:8080/uaf/reg/receive")
        var request = URLRequest(url: url!)
        request.httpBody = body
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            if error != nil {
                return
            }
            guard let result = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary else {
                return
            }
            let urlj = UrlJSon(dict: result as! [String : Any])
            print(urlj.statusCode)
            print(urlj.uafRequest!)
        }.resume()
    }
    
    func test1() -> Void {
        let url = URL(string: "https://bus.nationauth.cn/facets/facets.uaf")
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil {
                return
            }
            guard let result = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary else {
                return
            }
            print(result)
        }.resume()
    }
}
